package com;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;

public class Teacher {
	static int width = Toolkit.getDefaultToolkit().getScreenSize().width; // 获得屏幕宽带
	static int height = Toolkit.getDefaultToolkit().getScreenSize().height; // 获得屏幕高度

	public static void main(String[] args) {
		try {
			DatagramSocket ds = new DatagramSocket();
			InetAddress addr = InetAddress.getByName("255.255.255.255");
			
			new Thread() {
				@Override
				public void run() {
					while (true) {
						try {
							Robot robot = new Robot();
							BufferedImage image = robot.createScreenCapture(new Rectangle(width, height));
							ByteArrayOutputStream os = new ByteArrayOutputStream();  
							ImageIO.write(image, "png", os);  
							InputStream is = new ByteArrayInputStream(os.toByteArray()); 
							int c=0;
							byte[] buf=new byte[8192];
							while((c=is.read(buf))!=-1){
								DatagramPacket dp = new DatagramPacket(buf, c, addr, 54123);
								ds.send(dp);
							}
							DatagramPacket dp = new DatagramPacket("end".getBytes(), 3, addr, 54123);
							ds.send(dp);
							Thread.sleep(40);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}.start();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
